version 13.0
log using "C:\Users\mgolder\Dropbox\campaign_sentiment_paper\replication\data_analysis\figure4.log", replace
#delimit ;

*     ***************************************************************** *;
*     ***************************************************************** *;
*       File-Name:      figure4.do                                      *;
*       Date:           March 9, 2017                                   *;
*       Author:         MRG/CC                                          *;
*       Purpose:      	Produce Figure 4 for It's Not Only What You     *;
*                       Say, It's Also How You Say It: The Strategic    *;
*                       Use Of Campaign Sentiment. Journal of Politics. *;
*                       Forthcoming.                                    *;
* 	    Input File:     sentiment.dta                                   *;
*       Output File:    figure4.log                                     *;
*       Data Output:    none                                            *;             
*       Previous file:  sentiment.dta                                   *;
*       Machine:        desktop/laptop                     				*;
*     ****************************************************************  *;
*     ****************************************************************  *;

*     ****************************************************************  *;
*        Load data                                                      *;
*     ****************************************************************  *;

use "C:\Users\mgolder\Dropbox\campaign_sentiment_paper\replication\data_analysis\sentiment.dta", clear;

set more off;

*     ****************************************************************  *;
*       Declare data to be panel, where the panel is based on language  *;
*     ****************************************************************  *;

xtset language;

xtsum positive_sentiment;

*     ****************************************************************  *;
*       Generate additional variables that we will need for our         *;
*       analysis.                                                       *;
*     ****************************************************************  *;

gen incumbent_inflation_l = incumbent*inflation_l;
label var incumbent_inflation "incumbent*inflation_l";
gen incumbent_unemployment_l = incumbent*unemployment_l;
label var incumbent_unemployment_l "incumbent*unemployment_l";
gen incumbent_pm = incumbent*pm;
label var incumbent_pm "incumbent*pm";
gen incumbent_fm = incumbent*fm;
label var incumbent_fm "incumbent*fm";
gen left_right2=left_right*left_right;
label var left_right2 "left_right*left_right";
gen extremist = 0;
replace extremist=1 if family_id ==14 | family_id==40;
replace extremist = . if family_id==.;
label var extremist "1 if communist/socialist or far right, 0 otherwise";
gen extremist2 = 0;
replace extremist2=1 if family_id ==14 | family_id==40 | family_id==19;
replace extremist2 = . if family_id==.;
label var extremist2 "1 if communist/socialist, far right, or greens, 0 otherwise";
gen extremist_inflation_l=extremist*inflation_l;
label var extremist_inflation "extremist*inflation_l";
gen extremist_unemployment_l=extremist*unemployment_l;
label var extremist_unemployment_l "extremist*unemployment_l";

tabulate language, gen(l);

*     ****************************************************************  *;
*      Change graph scheme	                                            *;
*     ****************************************************************  *;

ssc install scheme-burd, replace;

*     ****************************************************************  *;
*     ****************************************************************  *;
*       Produce Figure 4                                                *;
*     ****************************************************************  *;
*     ****************************************************************  *;

*reg positive_sentiment incumbent incumbent_pm left_right left_right2 l1 l3 l4 l5 l6 l7, cluster(election_id);

bootstrap, reps(400) seed(10101) cluster(election_id): xtreg positive_sentiment incumbent incumbent_pm left_right left_right2, fe;

preserve;

set obs 10000;

set seed 10101;

drawnorm MG_b1-MG_b5, n(10000) means(e(b)) cov(e(V)) clear;

postutil clear;

postfile mypost y_hat lo hi using "C:\Users\mgolder\Dropbox\campaign_sentiment_paper\replication\data_analysis\sim.dta", replace;

noisily display "start";

local a=0 ;

while `a' <= 10 { ;

{;

scalar h_constant=1;

generate y  = MG_b5*h_constant + MG_b3*`a' + MG_b4*`a'*`a';

egen yhat = mean(y);

tempname y_hat lo hi;

_pctile y, p(2.5,97.5);
scalar `lo' = r(r1);
scalar `hi' = r(r2);

scalar `y_hat'=yhat;

post mypost (`y_hat') (`lo') (`hi');

};

drop y yhat;

local a=`a'+ 0.001;

display "." _c;

} ;

display "";

postclose mypost;

restore;

merge using "C:\Users\mgolder\Dropbox\campaign_sentiment_paper\replication\data_analysis\sim.dta";

gen MV=(_n-1)/1000;

replace MV=. if _n>10000;

ssc install scheme-burd, replace;

graph twoway hist left_right, percent lcolor(white) color(gs13) yaxis(2)
    || line y_hat MV, lwidth(medthick) clpattern(solid) color(black)
    || line lo MV, clpattern(longdash) clwidth(thin) clcolor(ebblue)
    || line hi MV, clpattern(longdash) clwidth(thin) clcolor(ebblue)
    ||,
    xlabel(0 1 2 3 4 5 6 7 8 9 10, nogrid labsize(2.5))
    ylabel(0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2 2.2, axis(1) nogrid labsize(2.5))
    ylabel(0 5 10 15 20, tlcolor(gs13) labcolor(gs13) axis(2) nogrid labsize(2.5))
    yscale(noline alt) yscale(noline alt axis(2)) xscale(noline) legend(off)
    ytitle("Positive Sentiment", size(3))
    ytitle("Percentage of Observations", color(gs12) axis(2) size(3))
    title("", size(3)) subtitle("", size(3)) xtitle("Left-Right Position", size(3)) xscale(titlegap(2)) yscale(titlegap(2)) scheme(burd);
        
graph export "C:\Users\mgolder\Dropbox\campaign_sentiment_paper\fig4.pdf", replace;

*     ****************************************************************  *;
*       Replication complete                                            *;
*     ****************************************************************  *;

log close;

clear;
